<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/easyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/easyui/themes/icon.css">
<script type="text/javascript" src="${pageContext.request.contextPath}/static/easyui/jquery.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/easyui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/easyui/locale/easyui-lang-zh_CN.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>角色管理</title>
</head>
<body>
<table id="roleTable" fit="true"></table>
<div id="tb">
	<a class="easyui-linkbutton" iconCls="icon-add" onclick="addRole()" plain="true">新增</a>
	<a class="easyui-linkbutton" iconCls="icon-edit" onclick="editRole()" plain="true">修改</a>
	<a class="easyui-linkbutton" iconCls="icon-remove" onclick="removeRole()" plain="true">删除</a>
</div>

<div id="roleDialog">
	<form id="roleForm" method="post">
		<table align="center" cellspacing="5px"  cellpadding="6px" style="margin-top: 15px;">
			<tr >
				<td >角色名称: </td>
				<td>
					<input type="text" id="roleName" name="roleName" >
					<input type="hidden" id="roleId" name="roleId"/>
					<span style="color: red">*</span>
				</td>
			</tr>
		</table>
	</form>
</div>
<div id="tt">
	<a class="easyui-linkbutton" iconCls="icon-save" plain="true" onclick="save()">保存</a>
	<a class="easyui-linkbutton" iconCls="icon-cancel" plain="true" onclick="cancel()">取消</a>
</div>

</body>
<script type="text/javascript">
$(function(){
	$("#roleTable").datagrid({
		url:"${pageContext.request.contextPath}/getAllRoleByPage",
		title:"角色管理",
		toolbar:"#tb",
		fitColumns:true,
		rownumbers:true,
		pagination:true,
		//singleSelect:true,
		columns:[[
			{field:'ck',checkbox:"true"},
			{field:"roleId",title:"编号",width:2,align:"center"},
			{field:"roleName",title:"角色名称",width:3,align:"center"}
		]]
	})
	
	$("#roleDialog").dialog({
			width:400,
			height:250,
			buttons:"#tt",
			closed:true
	})
})
	function addRole(){
		$("#roleForm").form("clear");
		$("#roleDialog").dialog("open");
		$("#roleDialog").dialog("setTitle","添加");
	}
	
	function editRole(){
		var data = $("#roleTable").datagrid("getSelections");
		if(data.length==0){
			$.messager.alert("温馨提示","请选择要修改的用户!","info");
		}else if(data.length>1){
			$.messager.alert("温馨提示","请选择一条要修改的用户!","info");
		}else{
			$("#roleForm").form("clear");
			$("#roleDialog").dialog("open");
			$("#roleDialog").dialog("setTitle","修改");
			$.ajax({
				async:false,
				type:"post",
				url:"${pageContext.request.contextPath}/getRoleByRoleId",
				data:{
					roleId:data[0].roleId
				},
				success:function(data){
					data=$.parseJSON(data).role;
					console.log(data);
					$("#roleId").val(data.roleId);
					$("#roleName").val(data.roleName);
				}
			})
		}
	}

	function removeRole(){
		var data = $("#roleTable").datagrid("getSelections");
		if(data.length>0){
			$.messager.confirm("温馨提示","是否删除?",function(r){
				if(r){
					var arrId = [];
					for(var i in data){
						arrId.push(data[i].roleId);
					}
					console.log(arrId)
					$.ajax({
						url:"${pageContext.request.contextPath}/removeRoleByArrId",
						type:"post",
						async:true,
						traditional:true,
						data:{
							arrId:arrId
						},
						success:function(data){
							if(data=='success'){
								$.messager.alert("温馨提示","删除成功!","info",function(){
									$("#roleTable").datagrid("load");
								});
							}else{
								$.messager.alert("温馨提示","删除失败!","info");
							}
						}
					})
				}
			})
		}else{
			$.messager.alert("温馨提示","请选中要删除的数据","info");
		}
	}

	
	function save(){
		var url;
		var id = $("#roleId").val();
		if(id){
			url='${pageContext.request.contextPath}/updateRoleByRole';
		}else{
			url='${pageContext.request.contextPath}/addRole';
		}
		$("#roleForm").form("submit",{
			url:url,
			onSubmit:function(){
				if($("#roleLoginname").val()==""){
					$.messager.alert("","登录用户名不能为空!","info");
					return false;
				}else if($("#rolePassword").val()==""){
					$.messager.alert("","密码不能为空!","info");
					return false;
				}
			},
			success:function(data){
				var i ;
				if(data=='success'){
					i = $.messager.alert("温馨提示","操作成功!","info",function(){
						$("#roleDialog").dialog("close");
						$("#roleTable").datagrid("load");
					});
					//2秒后自动关闭确认框
					setTimeout(function(){
						i.window("close");
						$("#roleDialog").dialog("close");
						$("#roleTable").datagrid("load");
					}, 2000);
				}else if (data == 'exist'){
					i = $.messager.alert("温馨提示","登录用户名已存在,请重新填写!","info");
					setTimeout(function(){
						i.window("close");
					}, 2000);
				}
				else{
					i = $.messager.alert("温馨提示","操作失败!","info");
					setTimeout(function(){
						i.window("close");
					}, 2000);
				}
			}
		});
	}
	function cancel(){
		$("#roleDialog").dialog("close");
	}








</script>
</html>